#include "qcommunicatedialog.h"
#include "ui_qcommunicatedialog.h"

QCommunicateDialog::QCommunicateDialog(const tagSerialConfig &config, QWidget *parent) :
    QBaseDialog(parent),
    ui(new Ui::QCommunicateDialog),
    m_config(config),
    m_ret(0)
{
    ui->setupUi(this);

    setProperty("no-ManhattanStyle",true);

    ui->rate->setCurrentIndex(ui->rate->findText(QString::number(config.m_baud_rate)));
    ui->data_length->setCurrentIndex(ui->data_length->findText(QString::number(config.m_data_length)));
    ui->stop->setCurrentIndex(ui->stop->findText(QString::number(config.m_stop)));
    if(config.m_parity=='n')
    {
        ui->parity->setCurrentIndex(0);
    }
    else if(config.m_parity=='o')
    {
        ui->parity->setCurrentIndex(1);
    }
    else if(config.m_parity=='e')
    {
        ui->parity->setCurrentIndex(2);
    }
    else
    {
        ui->parity->setCurrentIndex(0);
    }
    ui->serial_nummber->setValue(config.m_serial_number);

}

QCommunicateDialog::~QCommunicateDialog()
{
    delete ui;
}

int QCommunicateDialog::get_ret()
{
    return m_ret;
}

tagSerialConfig QCommunicateDialog::get_config()
{
    return m_config;
}

void QCommunicateDialog::on_okBtn_clicked()
{
    m_config.m_serial_number=ui->serial_nummber->value();
    m_config.m_baud_rate=ui->rate->currentText().toInt();
    m_config.m_data_length=ui->data_length->currentText().toInt();
    m_config.m_stop=ui->stop->currentText().toInt();
    int c=ui->parity->currentIndex();
    if(c==0)
    {
        m_config.m_parity='n';
    }
    else if(c==1)
    {
        m_config.m_parity='o';
    }
    else if(c==2)
    {
        m_config.m_parity='e';
    }
    else
    {
        m_config.m_parity='n';
    }
    m_ret=1;
    close();
}

void QCommunicateDialog::on_cancelBtn_clicked()
{
    close();
}
